package gui.panel;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.swing.DefaultListModel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

import Signals.TextMessage;
import network.NetworkInterface;


public class MainTabs extends JTabbedPane {

	private final static Color NEW_MESSAGE_COLOR = Color.getHSBColor(244, 91, 97);
	private final static Color DEFAULT_COLOR = Color.GRAY;
	private NetworkInterface networkInterface;
	private LinkedHashMap<String, ConversationPanel> currentConversationsMap;
	private HomePanel homePanel;
	
	public MainTabs(NetworkInterface networkInterface) {
		this.networkInterface = networkInterface;
		this.homePanel = new HomePanel(this, this.networkInterface);
		this.networkInterface.setMainTabs(this);
		this.setCurrentConversationsMap(new LinkedHashMap<String,ConversationPanel>()); 
		this.initComponents();
		this.networkInterface.startReceiving();
	}
	
	private void initComponents() {
		this.addComponents();
	}

	private void addComponents() {
		// TODO Auto-generated method stub
		this.addTab("Home", this.homePanel);
	}
	
	public LinkedHashMap<String, ConversationPanel> getCurrentConversationsMap() {
		return currentConversationsMap;
	}

	public void setCurrentConversationsMap(LinkedHashMap<String, ConversationPanel> currentConversationsMap) {
		this.currentConversationsMap = currentConversationsMap;
	}
	
	public ConversationPanel addNewConversation (String nickname) {
		ConversationPanel conversationPanel = new ConversationPanel(this.networkInterface);
		((DefaultListModel)(conversationPanel.getListInterlocutors().getModel())).addElement(nickname);
		this.addTab(this.networkInterface.parseUserName(nickname), conversationPanel);
		this.currentConversationsMap.put(nickname, conversationPanel);
		return conversationPanel;
	}
	
	public HomePanel getHomePanel() {
		return homePanel;
	}

	public void setHomePanel(HomePanel homePanel) {
		this.homePanel = homePanel;
	}

	public Boolean conversationExists(String nickname) {
		Boolean b = false;
		for (String mapKey : this.currentConversationsMap.keySet()) {
			if(mapKey.equals(nickname)) {
				b = true;
				break;
			}
				
		}
		return b;
	}
	
	public void focusOnTab(String nickname) {
		List<String> keys = new ArrayList<String>(this.currentConversationsMap.keySet());
		for (int i = 0; i < keys.size(); i++) {
		    if (keys.get(i) == nickname) {
		    	this.setSelectedIndex(i+1);
		    	break;
		    }
		}
	}
	
	public void colorOnTab(Color color, String nickname) {
		List<String> keys = new ArrayList<String>(this.currentConversationsMap.keySet());
		for (int i = 0; i < keys.size(); i++) {
		    if (keys.get(i) == nickname) {
		    	this.setBackgroundAt(i+1, color);
		    	break;
		    }
		}
		
	}
	
	public void setNewMessageTab(String nickname) {
		colorOnTab(NEW_MESSAGE_COLOR, nickname);
	}
	
	public void setDefaultTab(String nickname) {
		colorOnTab(DEFAULT_COLOR, nickname);
	}
	
	
	
	public void displayNewMessageReceived (String nickname, TextMessage textMessage) {
		String header = this.networkInterface.parseUserName(nickname) + ": ";

		if(conversationExists(nickname)) {
			JTextArea txtChatHistory = this.currentConversationsMap.get(nickname).getTxtChatHistory();
			txtChatHistory.setText(txtChatHistory.getText() + '\n' + header + textMessage.getMessage());
			
		} else {
			JTextArea txtChatHistory = this.addNewConversation(nickname).getTxtChatHistory();
			txtChatHistory.setText(header + textMessage.getMessage());
		}
	}
}
